import {BasicColumn, FormSchema} from '/@/components/Table';
import {render} from '/@/utils/common/renderUtils';
import {DescItem} from '/@/components/Description/index';
import {ChannelTypeEnum} from './task.model';
import {JVxeColumn, JVxeTypes} from '/@/components/jeecg/JVxeTable/types'
import {listDataGroupTree} from '/@/api/infra/dataGroup';
import {DataGroupBizType} from "/@/components/BizDataGroup/index"

export const columns: BasicColumn[] = [
  {
    title: '任务名称',
    dataIndex: 'name',
    width: 120,
  },  

  {
    title: '任务编码',
    dataIndex: 'taskCode',
    width: 130,
    // slots: { customRender: 'taskName' }
  },

  {
    title: '功能组',
    dataIndex: 'groupName',
    width: 80,
  },  

  {
    title: '创建时间',
    dataIndex: 'createdAt',
    sorter: true,
    width: 160
  },
  {
    title: '创建者',
    dataIndex: 'createdBy',
    sorter: true,
    width: 120
  },
  {
    title: '状态',
    dataIndex: 'status',
    width: 60,
    customRender: ({ text }) => {
      const color = text === '1' ? 'green' : 'red';
      return render.renderTag(render.renderDict(text, 'data_use_status'), color)
    }
  },

];

export const receiverColumns : BasicColumn[] = [
  {
    title: '姓名',
    dataIndex: 'name',
    width: 100,
  },
  {
    title: '手机号',
    dataIndex: 'phone',
    slots: { customRender: 'phone' },
    width: 120,
  },
  {
    title: '邮箱',
    dataIndex: 'email',
    slots: { customRender: 'email' },
    width: 150,
  },
  {
    title: '备注',
    dataIndex: 'remark',
    width: 100,
  }
];

export const searchFormSchema: FormSchema[] = [
  {
    field: 'keyword',
    label: '任务',
    component: 'Input',
    componentProps: {
      placeholder: '任务名称/编码',
    },
    colProps: { span: 8 },
  },
  {
    label: '功能组',
    field: 'groupId',
    component: 'ApiSelect',
    componentProps: {
      api: listDataGroupTree,
      params: {
        bizType: DataGroupBizType.MSG_PUSH_TASK,
      },
      optionFilterProp: "label",
      showSearch: true,
      labelField: 'name',
      valueField: 'id'
    },
    colProps: {span: 6},
  },
  {
    field: 'status',
    label: '状态',
    component: 'JDictSelectTag',
    componentProps: {
      dictCode:'data_use_status',
      placeholder:'请选择状态',
      stringToNumber: true
    },
    colProps: { span: 8 },
  },
];

export const formSchema: FormSchema[] = [
  {
    field: 'id',
    label: 'id',
    component: 'Input',
    show: false,
    required: false
  },
  {
    field: 'name',
    label: '任务名称',
    component: 'Input',
    required: true
  },
  {
    field: 'taskCode',
    label: '任务编码',
    dynamicDisabled: ({values}) => {
      return !!values.id
    },
    component: 'Input',
    required: true
  },

  {
    label: '消息功能组',
    field: 'groupId',
    component: 'ApiSelect',
    componentProps: {
      api: listDataGroupTree,
      params: {
        bizType: DataGroupBizType.MSG_PUSH_TASK,
      },
      optionFilterProp: "label",
      showSearch: true,
      labelField: 'name',
      valueField: 'id',
      dropdownStyle: { maxHeight: '16vh', overflow: 'auto' },
    }
  },

  {
    field: 'status',
    label: '状态',
    defaultValue: '1',
    component: 'JDictSelectTag',
    componentProps: {
      type: "radio",
      dictCode:'data_use_status',
      placeholder:'请选择状态'
    },
  },

  {
    field: 'receiverType',
    label: '接收者类型',
    defaultValue: '1',
    // helpMessage: '如果是会员通知, 则在代码中可以为任意用户推送',
    component: 'JDictSelectTag',
    componentProps: {
      type: "radio",
      dictCode:'msg_receiver_type',
    },
  },

  {
    label: '备注',
    field: 'remark',
    component: 'InputTextArea',
    componentProps: {
      placeholder: '请输入备注',
      rows: 3,
    },
  }
];


export const formDetailSchema: DescItem[] = [
  {
    field: 'name',
    label: '任务名称',
  },
  {
    field: 'taskCode',
    label: '任务编码',
  },
  {
    field: 'groupName',
    label: '功能组',
  },

  {
    field: 'status',
    label: '状态',
    render: (curVal, data) => {
      const color = curVal === '1' ? 'green' : 'red';
      return render.renderTag(render.renderDict(curVal, 'data_use_status'), color)
    }
  },
  {
    field: 'createdAt',
    label: '创建时间'
  },
  {
    field: 'createdBy',
    label: '创建者'
  },
  {
    field: 'updatedAt',
    label: '更新时间'
  },
  {
    field: 'updatedBy',
    label: '更新者'
  },
  {
    label: '备注',
    labelMinWidth: 40,
    field: 'remark',
  }
];



export const channelColumns: BasicColumn[] = [
  {
    title: '渠道类型',
    dataIndex: 'channelTypeName',
    width: 100,
  },
  {
    title: '渠道名称',
    dataIndex: 'channelName',
    width: 160,
  }
];

export const templateSchema: FormSchema[] = [
  {
    // 小程序或者PC推送才拥有
    field: 'pageUrl',
    label: '页面地址',
    helpMessage: '点击推送的消息将会跳转到该地址',
    required: false,
    ifShow: ({ values }) => {
      return values.channelType === ChannelTypeEnum.PC || values.channelType === ChannelTypeEnum.WX_MA
    },
    component: 'Input',
  },
  {
    field: 'channelType',
    label: 'channelType',
    show: false,
    component: 'Input',
  },
  {
    field: 'templateId',
    label: '模板ID',
    ifShow: ({ values }) => {
      return values.channelType !== ChannelTypeEnum.EMAIL
    },
    required: ({ values }) => values.channelType !== ChannelTypeEnum.EMAIL,
    component: 'Input',
  },
];


export const msgFieldColumns: BasicColumn[] = [
  {
    title: '变量',
    dataIndex: 'isVar',
    width: 60,
    customRender: ({ text }) => {
      return text ? '是' : '否';
    }
  },
  {
    title: '字段描述',
    dataIndex: 'fieldDesc',
    width: 130,
  },
  {
    title: '字段名称',
    dataIndex: 'fieldName',
    width: 120,
  },  
  {
    title: '字段值',
    dataIndex: 'fieldValue',
    width: 140,
  }, 
];


/**
 * 消息字段
 */
export const msgFieldVxeColumns : JVxeColumn[] = [
  // 表的列id
  { title: 'ID', key: 'id', type: JVxeTypes.hidden },
  { 
    title: '变量', key: 'isVar', type: JVxeTypes.checkbox, width: 60, defaultChecked: true,
    validateRules: [{ required: true, message: '${title}不能为空' }],
  },
  { 
    title: '字段名称', key: 'fieldName', type: JVxeTypes.input, width: 210, defaultValue: '',
    validateRules: [{ required: true, message: '${title}不能为空' }],
  },
  { 
    title: '字段描述', key: 'fieldDesc', type: JVxeTypes.input, width: 210, defaultValue: '',
    validateRules: [{ required: true, message: '${title}不能为空' }],
  },
  { 
    title: '字段值', key: 'fieldValue', type: JVxeTypes.input, defaultValue: ''
  },
]
